<template>
    <div style="margin-top: 100px;width: 1200px">

        <div>
            <el-form :model="form"
                     style="display:inline-block;vertical-align: top;margin-right: 80px"
                     label-width="150px">
                <el-form-item label="商家名称：">
                    <el-input style="width: 300px;" v-model="form.storeName" :maxlength="50"></el-input>
                </el-form-item>
                <el-form-item label="商家主账号：">
                  <span>{{form.contactNum}}</span>
                  <el-button type="info" size="small" style="margin-left: 20px;" @click="changePwd()">修改密码</el-button>
                </el-form-item>
                <el-form-item label="商家地址：">
                    <span>{{ form.storeAddress }}</span>
                </el-form-item>
                <el-form-item label="商家营业时间：" label-width="150px" style="margin-top: 25px;">          
                           <el-time-select
                            placeholder="起始时间"
                            v-model="startTime"
                            :picker-options="{
                              start: '00:00',
                              step: '00:15',
                              end: '23:45'
                            }">
                          </el-time-select>
                          <el-time-select
                            placeholder="结束时间"
                            v-model="endTime"
                            :picker-options="{
                              start: '00:00',
                              step: '00:15',
                              end: '23:45'
                            }">
                          </el-time-select>
                </el-form-item>
                <el-form-item label="商家类型：">
                    <span>{{ findSort(form.storeType) }}</span>
                </el-form-item>
                <el-form-item label="商家分类：">
                    <el-select v-model="form.businessClass" style="width: 200px;"  @change="selectSort2()"
                           placeholder="一级分类">
                    <el-option :label="item.classifyName" :value="item.classifyId"
                               v-for="item in sortList"></el-option>
                    </el-select>
                    <el-select v-model="form.businessSubclass" style="width: 200px;" placeholder="二级分类" >
                        <el-option :label="item.classifyName" :value="item.classifyId"
                                   v-for="item in secondList"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="门店电话：">
                    <el-input style="width: 300px;" v-model="form.storePhone" :maxlength="50"></el-input>
                </el-form-item>
                <el-form-item label="商家简介：">
                     <el-input style="width: 300px;" v-model="form.introduction"  type="textarea" :rows="5"></el-input>
                </el-form-item>
                <el-form-item label="">
                   <el-button type="primary" :loading="loading" @click="submitForm()">保存</el-button> 
                   <el-button type="success" :loading="loading" @click="activate()" v-if="form.actStat==0">激活</el-button> 
                </el-form-item>
                 
            </el-form>
            <el-form label-width="120px" class="demo-ruleForm" style="display:inline-block;;max-width:500px" :model="form" >
                <el-form-item label="补贴比例：">
                    <el-select v-model="form.storeRebate" style="width: 200px;margin-right: 30px;">
                        <el-option :label="item.text" :value="item.value" v-for="item in scalePublicData"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="商家封面图：" label-width="100px">
                  <div style="display: inline-block;text-align: center">
                       <we-ui-uploads :images="form.storeImg"
                                   :maxCount="1"
                                   :maxSize="1024*1024"

                                   @uploading="uploading"></we-ui-uploads>  
                                   （必传） 
                 </div>
                   
                                  
                </el-form-item>
                <el-form-item label="商家简介图：" label-width="100px">
                    <we-ui-uploads :images="form.storeImgList"
                                   :maxCount="9"
                                   :maxSize="1024*1024"
                                   :isSort="true"
                                   @uploading="uploading2"></we-ui-uploads>
                                  (推荐尺寸200*200,首张为主图,最多9张)
                </el-form-item>
            </el-form>

        </div>
        
          <!--修改密码-->
            <el-dialog title="修改密码" v-model="dialogTableVisible" size="tiny">
              <el-row>
                <el-col :span="24">
                    <el-form :inline="true"  class="demo-form-inline" label-width="120px">
                        <el-form-item label="原密码:">
                            <el-col :span="30">
                                <el-input v-model="old_password" type="password"></el-input>
                            </el-col>
                        </el-form-item>
                         <el-form-item label="新密码:">
                            <el-col :span="30">
                                <el-input v-model="new_password" type="password"></el-input>
                            </el-col>
                        </el-form-item>
                         <el-form-item label="确认新密码:">
                            <el-col :span="30">
                                <el-input v-model="check_password" type="password"></el-input>
                            </el-col>
                        </el-form-item>
                    </el-form>
               </el-col>
            </el-row>        
              <div style="text-align:center">
              <el-button type="primary" @click="editPwd()" :loading="loading2">确定</el-button>
              </div>
            </el-dialog> 


    </div>

</template>
<style>

</style>

<script>
    export default {
        data() {
            return {
                loading: false,
                loading2: false,
                form: {
                    storeId:'',
                    storeName:'',
                    storePhone:'',
                    storeAddress:'',
                    businessTime:'',
                    businessClass:'',
                    businessSubclass:'',
                    introduction:'',
                    businessLicense: [],
                    idcardFront: [],
                    idcardBack: [],
                    storeImg: [],
                    storeImgList:[],
                    contactNum:'',
                    storeRebate:0.08

                },
                sortList: [],
                secondList: [],
                startTime:'',
                endTime:'',
                userName:'',
                dialogVisible:false,
                id:'',
                show:'',
                dialogTableVisible:false,
                old_password:'',
                new_password:'',
                check_password:'',
                typeList:[{text:'物流商超',value:'1'},{text:'服务行业',value:'2'},{text:'酒店',value:'3'}],
                storeImgList:[],
                proportion: [{text: '0%', value: 0},{text: '4%', value: 0.04},{text: '6%', value: 0.06}, 
                {text: '8%',value: 0.08}, {text: '10%',value: 0.1},
                {text: '12%', value: 0.12}, {text: '14%', value: 0.14}, 
                {text: '16%',value: 0.16}]
            }
        },
        created(){
          
           this.selectSort()
           this.fetchData()
        },
        activated(){
        },
        methods: {
            fetchData(){
				
                if (this.$route.params.id==0) {
                    return;
                }
                this.dataApi.ajax('singleStoreInfo',null, res => {
                    if (res.responseType === 'S') {
                         this.form=res
                         this.form.storeImg ? this.form.storeImg=[this.form.storeImg]:[]
                         this.storeImgList=generateArr(res.storeImgList)
                         this.form.storeImgList=[]
                         for (var i = 0;i<this.storeImgList.length ; i++) {
                            this.form.storeImgList.push(this.storeImgList[i].imgUrl)
                         }
                         if (this.form.businessTime!='') {
                            this.startTime=this.form.businessTime.split('-')[0]
                            this.endTime=this.form.businessTime.split('-')[1] 
                         }
                         this.show=true

                    }
                });
            },
            submitForm(){
                if (!this.form.storeName) {
                   this.showMsg('请输入商家名称','warning')
                   return;
                } 
                if (!this.startTime|| !this.endTime) {
                   this.showMsg('请输入营业时间','warning')
                   return;
                 }
                if (!this.form.businessClass|| !this.form.businessSubclass) {
                   this.showMsg('请选择分类','warning')
                   return;
                 }


                 if (this.form.storeImg.length==0) {
                    this.showMsg('请上传商家封面图片','warning')
                    return false;
                }
                let data = generateObj(this.form)

                data.storeImg=data.storeImg.toString()
                
                var storeImgList=[]
                for (var i = data.storeImgList.length - 1; i >= 0; i--) {  
                    for (var j = this.storeImgList.length - 1; j >= 0; j--) { 
                        if (this.storeImgList[j].imgUrl==data.storeImgList[i]) {
                             storeImgList.push(this.storeImgList[j])
                             break;
                        }
                    }     
                }
                data.storeImgList=storeImgList.reverse()
                data.businessTime=this.startTime+'-'+this.endTime
                this.loading=true
                this.dataApi.ajax('editStoreInfoForPlatform', data, res => {
                    if (res.responseType === 'S') {
                        this.showMsg('保存成功')
                        this.loading=false
                        this.fetchData()
                    }
                }); 
                
            },
            selectSort(){
                this.dataApi.ajax('selectStoreClassify', {parentId:0}, res => {
                    if (res.responseType === 'S') {
                       this.sortList = res.dtos
                    }
                });

            },
            selectSort2(){
                if (this.show) {
                    this.form.businessSubclass= this.form.businessSubclass
                    this.show=false
                }else{
                    this.form.businessSubclass ='' 
                }
                this.dataApi.ajax('selectStoreClassify', {parentId: this.form.businessClass}, res => {
                    //console.log(res)
                    if (res.responseType === 'S') {
                        this.secondList = res.dtos
                    }
                });
                

            },
            uploading(f) {
                 this.loading=true
                 this.dataApi.upload('FileUploadNotify/upload', f.file, res => {
                        //console.log(res)
                        if (res.responseType == 'S') {
                            f.images.splice(f.index,1,res.filePath)
                            this.loading=false
                        }
                 });
                
            },
             uploading2(f) {
                 this.loading=true
                 this.dataApi.upload('FileUploadNotify/upload', f.file, res => {
                        //console.log(res)
                        if (res.responseType == 'S') {
                            f.images.splice(f.index,1,res.filePath)
                            this.storeImgList.push(
                            {
                                imgHeight:res.height,
                                imgUrl:res.filePath,
                                imgWidth:res.width
                            })
                            this.loading=false
                        }
                 });
                
            },
            changePwd(){
              this.dialogTableVisible=true
               this.old_password=''
               this.new_password=''
               this.check_password=''
            },
            editPwd () {

                 if (this.new_password!=this.check_password) {
                   this.$message({
                      type: 'warning',
                      message: '两次输入的密码不一致!'
                    });
                   return;
                 }
                 if (this.new_password.length<6) {
                            this.$message({
                              message: '密码不能少于6位',
                              type: 'warning'
                            });
                             return;
                }
                var data={
                   pwd:this.encrypt(this.old_password),
                   newPwd:this.encrypt(this.new_password),
                   userId:JSON.parse(sessionStorage.getItem('permission')).adminCode
                }
                this.loading2=true
                this.dataApi.ajax('resetPwdStorePlatform',data, res => {
                    if (res.responseType === 'S') {
                        this.showMsg('修改成功')
                        this.dialogTableVisible=false
                    }
                     this.loading2=false
                });

            },
            encrypt(str) {
                var code = 956132157;
                var result=''
                for (var i = 0; i < str.length; i++) {//遍历字符数组
                     result += (str.charCodeAt(i) ^ code);//对每个数组元素进行异或运算，异或的值可以自己选择
                }
                return result;
            },
            findSort(type){
                    for (var i = this.typeList.length - 1; i >= 0; i--) {
                        if (type==this.typeList[i].value) {

                            return this.typeList[i].text
                        }
                    }
            }

        }
    }
</script>
